<?php

namespace App\Services;

class AjaxResponse{
	/**
	 * @var array
	 */
	private $_return = [];

	/**
	 * 统一响应请求返回数据
	 *
	 * AjaxResponse constructor.
	 * @param int $code
	 * @param string $msg
	 * @param array $data
	 */
	public function __construct($code = 200, $msg = 'success', $data = []){
		if ($data){
			foreach ($data as $key => $value){
				$this->_return[$key] = $value;
			}
		}
		$this->_return['code'] = $code;
		$this->_return['msg'] = $msg;
		return $this->_return;
	}

	/**
	 * Ajax成功响应
	 *
	 * @param array $data
	 * @param int $code
	 * @param $msg
	 * @return AjaxResponse
	 */
	public function success($data = [], $code = 200, $msg = 'success'){
		return $this->__construct($code, $msg, $data);
	}

	/**
	 * Ajax失败响应
	 *
	 * @param int $code
	 * @param string $msg
	 * @return AjaxResponse
	 */
	public function fail($code = AJAX_CODE_PERMISSION_DENY_ERROR , $msg = ''){
		return $this->__construct($code, empty($msg) ? __('language.ajax_code_'.$code) : $msg);
	}
}